Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
@babel/types
Advanced tools
The @babel/types package is a part of the Babel compiler ecosystem. It contains methods for building and validating AST (Abstract Syntax Tree) nodes for JavaScript-like languages. It is commonly used for creating and transforming code within Babel plugins.
AST Node Creation
This feature allows the creation of AST nodes. The code sample demonstrates how to create an identifier and a numeric literal using the package.
const t = require('@babel/types');
const identifier = t.identifier('myVariable');
const numericLiteral = t.numericLiteral(123);
AST Node Validation
This feature is used to validate if a node is of a specific type. The code sample checks if a node is an identifier.
const t = require('@babel/types');
const isValid = t.isIdentifier(t.identifier('myVariable'));
AST Node Transformation
This feature allows the transformation of AST nodes. The code sample creates a binary expression node that represents 'a + b'.
const t = require('@babel/types');
const binaryExpression = t.binaryExpression('+', t.identifier('a'), t.identifier('b'));
AST Node Traversal
This feature is not directly provided by @babel/types but is often used in conjunction with it. It involves traversing the AST and updating nodes. The code sample renames an identifier within the AST.
const t = require('@babel/types');
const traverse = require('@babel/traverse').default;
const ast = t.file(t.program([t.expressionStatement(t.identifier('myVariable'))]));
traverse(ast, {
enter(path) {
if (t.isIdentifier(path.node)) {
path.node.name = 'newVariable';
}
}
});
Acorn is a small, fast, JavaScript-based JavaScript parser. It produces an abstract syntax tree similar to the one produced by @babel/types but does not include the same utilities for building or validating nodes.
Esprima is another JavaScript parser that produces an AST. It is used for static analysis and other code transformation tasks, similar to @babel/types, but it has its own API and does not provide the same helper functions for node creation and validation.
Recast is a JavaScript AST manipulation library that uses Esprima under the hood. It provides a different set of utilities for parsing, transforming, and printing code, and it preserves source formatting, which is different from @babel/types.
Babel Types is a Lodash-esque utility library for AST nodes
See our website @babel/types for more information or the issues associated with this package.
Using npm:
npm install --save-dev @babel/types
or using yarn:
yarn add @babel/types --dev
v7.26.0 (2024-10-25)
babel-core
, babel-generator
, babel-parser
, babel-plugin-syntax-import-assertions
, babel-plugin-syntax-import-attributes
, babel-preset-env
, babel-standalone
, babel-types
babel-core
babel-compat-data
, babel-plugin-proposal-regexp-modifiers
, babel-plugin-transform-regexp-modifiers
, babel-preset-env
, babel-standalone
babel-parser
startIndex
parser option (@DylanPiercey)babel-generator
, babel-parser
, babel-plugin-syntax-flow
babel-helpers
, babel-preset-typescript
, babel-runtime-corejs3
import()
in rewriteImportExtensions
(@liuxingbaoyu)babel-generator
, babel-parser
@babel/generator
(@nicolo-ribaudo)babel-core
babel-plugin-proposal-json-modules
, babel-plugin-transform-json-modules
, babel-standalone
proposal-json-modules
to transform-json-modules
(@nicolo-ribaudo)babel-code-frame
, babel-highlight
@babel/highlight
in @babel/code-frame
(@nicolo-ribaudo)babel-generator
, babel-parser
, babel-types
kind
to TSModuleDeclaration
(@liuxingbaoyu)babel-helper-module-transforms
, babel-plugin-transform-modules-commonjs
FAQs
Babel Types is a Lodash-esque utility library for AST nodes
The npm package @babel/types receives a total of 57,580,415 weekly downloads. As such, @babel/types popularity was classified as popular.
We found that @babel/types demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.